package com.zegome.game.gomestory.main.scene.draw;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.equations.Quart;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;
import com.zegome.facebook.IFacebookBridge;
import com.zegome.facebook.IFacebookProcessorListener;
import com.zegome.game.entity.sprite.Sprite;
import com.zegome.game.gomestory.main.D;
import com.zegome.game.gomestory.main.helper.IMAction;
import com.zegome.game.gomestory.main.libs.PNG;
import com.zegome.game.gomestory.main.scene.BaseGameScreen;
import com.zegome.game.main.AndroidGameController;
import com.zegome.game.screen.GameScreenOption;
import com.zegome.game.util.helper.WidgetAccessor;

public class DrawScreen extends BaseGameScreen{
	
	private Sprite bg, paper;
	private ImageButton createMap, fbShared;
	Label level;
	Sprite painter;
	ImageButton upgradeGome;
	private Table mapTable;
	private ScrollPane mapScroll;
	private Table[] mapItem;	
	private int currentItem = 1;
	private float scrollCurrentX = 0;
	
	private ImageButton cancel;
	private Table iconTable;
	private ScrollPane iconScroll;
	private boolean swipeAble;
	float xScrollBackup;
	int count;
	
	private ImageButton prev,next;
	
	int maxRow, maxCol;
	
	int indexHelp = 0;
	
	public DrawScreen(AndroidGameController controller, String name, final IFacebookBridge bridge) {
		super(controller, name,bridge);		
		setConfig(true, true, true,true);		
	}	
	
	public void show() {
		indexHelp = 0;
		ctr.a.soundOnOff();
		ctr.a.isHasDialog.removeAllElements();
		ctr.d.backLis.removeAllElements();
		ctr.d.getDataChapter();
		maxRow = 8;
		maxCol = 12;
		if(ctr.d.isLDPI){
			maxRow = 6;
			maxCol = 10;
		}
		isShowTut = false;
		state = 0;
		ctr.d.getMap();
		stage.clear();
		createUi();		
		super.show();
		ctr.a.bgSong.play();				
		if(ctr.d.passScreen[D.DRAW] == false){
			ctr.d.passScreen[D.DRAW] = true;
			ctr.d.saveAccount();
		}
	}

	public void createUi() {
		if(bg==null){
			bg = new Sprite(0, 0, ctr.a.bgDraw);
			bg.setSize((800), (480));
			attachChild(bg);			
		}
		
		addIconGomeSmall();
		
		if (cancel == null) {
			cancel = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.cancel,
					null, null, (747), (480-190));
			cancel.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					ctr.stageHelper.createDialog(stage, tweenManager,
						ctr.lang.deleteThisMap, 2,
						new ChangeListener() {							
							@Override
							public void changed (ChangeEvent event, Actor actor) {
								// TODO Auto-generated method stub
								ctr.d.maps.removeElementAt(currentItem-1);
								ctr.d.saveAllMap();
								createMapScroll();
							}
						}, null);					
				}
			});
		}
		
		if(level==null){
			level = ctr.stageHelper.createLabel(ctr.g.mFont, "", Color.WHITE, 0, 0);
			level.setText("Level "+(ctr.d.levelDraw>=5?"Max":""+ctr.d.levelDraw));
			level.setPosition((10), (480-230));
		}
		if(painter==null){
			painter = new Sprite(0, 0, ctr.a.gomePainter);
			painter.setSize((164), (130));
			painter.setPosition(0, (480-223));
			attachChild(painter);			
		}
		if(upgradeGome==null){
			upgradeGome = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.transRegion, null, null, 0, (480-223),164,130);			
			upgradeGome.addListener(new ChangeListener() {
				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.levelDraw<5){
					ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.upgradeGomeLevel, 2, 
							new ChangeListener() {
								
								@Override
								public void changed(ChangeEvent arg0, Actor arg1) {
									// TODO Auto-generated method stub	
									ctr.stageHelper.createDialogPayGoldDiamond(DrawScreen.this, stage, tweenManager,500,5000,new IMAction() {										
										@Override
										public void action() {
											// TODO Auto-generated method stub
											ctr.d.levelDraw++;
											ctr.d.saveAccount();
											ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.getLevelArtist(), 1, null, null);
										}
									});									
								}
							}, null);
					}
					else{
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.gomeMaxLevel, 1, 
								null, null);
					}
				}
			});
		}
		if(prev==null){
			prev = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.prev, ctr.a.prev, null, 200-63, 150);		
			prev.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(
							mapScroll.isPanning()||
							mapScroll.isDragging()||
							mapScroll.isFlinging()
					  ){}					
						currentItem --;
						if(currentItem<1)
							currentItem = 1;
						mapScroll.setScrollX((currentItem-1)*(520));									
				}
			});
		}
		if(next==null){
			next = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.next, ctr.a.next, null, 200+520, 150);		
			next.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(
							mapScroll.isPanning()||
							mapScroll.isDragging()||
							mapScroll.isFlinging()
					  ){}
					else{
						currentItem ++;
						if(currentItem>mapItem.length)
							currentItem = mapItem.length;
						mapScroll.setScrollX((currentItem-1)*(520));
					}
				}
			});
		}
		
		if(createMap==null){			
			boolean isHasMap = (ctr.d.maps.size()>0)?true:false;
			createMap = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.createMapNor, ctr.a.createMapPress, null, 0, 0,isHasMap?145:145*2,isHasMap?43:43*2);		
			if(ctr.d.maps.size()>0)
				createMap.setPosition((800-182), (350+5));
			else{
				createMap.setPosition(300, (480-300));				
			}
			if(isHasMap == false){
				if(paper == null){
					paper = new Sprite(146, 8, ctr.a.grid);
					//paper.setColor(paper.getColor().r, paper.getColor().g, paper.getColor().b, 0.1f);		
					paper.setSize(599, 398);
					attachChild(paper);
				}	
			}
			createMap.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;					
				fadeOut(new TweenCallback() {
					@Override
					public void onEvent(int type, BaseTween<?> source) {
						ctr.a.bgSong.pause();
						clear();
						mController.setScreen(mController
								.getScreen("Draw2Screen"));						
					}
				});	
				}
			});
		}
								
		createIconScroll();
				
		stage.addActor(level);
		stage.addActor(iconScroll);
		createMapScroll();
		stage.addActor(cancel);
		
		stage.addActor(createMap);
		stage.addActor(prev);
		stage.addActor(next);
		stage.addActor(upgradeGome);
		Table avatar = ctr.stageHelper.createAvatar(this,stage, tweenManager, (0), (480-99), 1f);
		stage.addActor(avatar);
	}
	
	private void createMapScroll () {
		// TODO Auto-generated method stub
		swipeAble = false;
		if(mapItem != null && mapItem.length>0){
			for(int i=0;i<mapItem.length;i++){
				mapItem[i].remove();
				mapItem[i].clear();
				mapItem[i] = null;
			}
			mapItem = null;			
		}
		
		if(mapTable !=null){
			mapTable.remove();
			mapTable.clear();
			mapTable = null;
		}
		
		if(mapScroll != null){
			mapScroll.remove();
			mapScroll.clear();
			mapScroll = null;
		}
		
		if(fbShared!=null){
			fbShared.remove();
			fbShared = null;
		}
		
		mapItem = new Table[ctr.d.maps.size()];
		
		for(int i = 0; i<mapItem.length;i++){
			createMapItem(i);
		}
		
		if(mapTable == null){
			mapTable = ctr.stageHelper.createTable(ctr.g.mSkin, 0, 0, (520), (363));			
			mapTable.row();
			for(int i = 0; i<mapItem.length;i++){
				mapTable.add(mapItem[i]);
			}
		}
		
		if(mapScroll == null){
			mapScroll = new ScrollPane(mapTable);
			mapScroll.setBounds((200), (480-445), (520), (308+55));
			mapScroll.setFlingTime(0);
		}
		
		fbShared = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.fbShared, null, null, 180, 300, 56, 56);
		fbShared.addListener(new ChangeListener() {			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				// TODO Auto-generated method stub				
				if(ctr.a.isHasDialog() == true||ctr.a.isFbAction)
					return;
				ctr.a.isFbAction = true;
				final Bitmap photo = PNG.getFrameBufferTexture(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
				ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.shareThisMap, 2,
						new ChangeListener() {
							
							@Override
							public void changed(ChangeEvent arg0, Actor arg1) {
								// TODO Auto-generated method stub
								postRunnable(new Runnable() {
									
									@Override
									public void run() {
										// TODO Auto-generated method stub
										//final Bitmap photo = PNG.getFrameBufferTexture(0, 0, 800, 480);
										if(ctr.d.isLoginedFB){
											mAction = IFacebookProcessorListener.PUBLISH_STORY;
											mFBBridge.onOpenAction(DrawScreen.this, photo);
										}
										else{
											afterAction = new IMAction() {
												
												@Override
												public void action() {
													// TODO Auto-generated method stub
													mAction = IFacebookProcessorListener.PUBLISH_STORY;
													mFBBridge.onOpenAction(DrawScreen.this, photo);
												}
											};
											mAction = IFacebookProcessorListener.LOGIN;
											mFBBridge.onLogin(DrawScreen.this);
										}
									}
								});
							}
						}, new ChangeListener() {
							
							@Override
							public void changed(ChangeEvent arg0, Actor arg1) {
								// TODO Auto-generated method stub
								ctr.a.isFbAction = false;
							}
						});
			}
		});
		
		stage.addActor(mapScroll);
		
		if(!ctr.d.maps.isEmpty())
			stage.addActor(fbShared);
		
		if(createMap!=null)
			createMap.remove();
		stage.addActor(createMap);
		
		swipeAble = true;
	}

	public void createMapItem(final int index)
	{
		TextureRegion bgRegion;
		int[][] mapData;
		bgRegion = ctr.d.maps.elementAt(index).bg;
		mapData = getMap(ctr.d.maps.elementAt(index).map);
		float xBegin = (68);
		float yBegin = (26);
		
		float wiItem = (32);
		float hiItem = (32);
		
		if(ctr.d.isLDPI){
			wiItem = 38;
			hiItem = 42;
		}
		
		if(mapItem[index] == null){			
			mapItem[index] = ctr.stageHelper.createTableWithTransBg(ctr.g.mSkin, 0, 0, (520), (308+45));						
		}
		
		//mapItem[index].clear();
		Label name;
		Image bgName;
		bgName = ctr.stageHelper.createImage(ctr.a.bgNameMap, 0, 0, 200,43);
		bgName.setPosition((520-bgName.getWidth())/2, 315);
		name = ctr.stageHelper.createLabel(ctr.g.mFont, "", Color.WHITE, 0, 0);
		name.setText(ctr.d.maps.elementAt(index).name);
		name.setPosition((50), (331));	
		name.setX(((520)-name.getTextBounds().width)/2);
		mapItem[index].addActor(bgName);
		mapItem[index].addActor(name);
		Image bgMap = ctr.stageHelper.createImage(bgRegion, 0, 0, (520), (308));
		
		mapItem[index].addActor(bgMap);			
			for(int i=0;i<maxRow;i++){
				for(int j=0;j<maxCol;j++){
					if(mapData[i][j] == 1){
						Image temp = ctr.stageHelper.createImage(ctr.d.maps.elementAt(index).square, xBegin+j*wiItem, yBegin+i*hiItem, wiItem, hiItem);
						//ctr.d.maps.elementAt(index).item
						Image icon = ctr.stageHelper.createImage((ctr.d.maps.elementAt(index).item), xBegin+j*wiItem, yBegin+i*hiItem, wiItem, hiItem);
						mapItem[index].addActor(temp);
						mapItem[index].addActor(icon);
					}
				}
			}
			
			ImageButton but = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.transRegion, null, null, 0, 0, (520), (308));
			
			mapItem[index].addActor(but);			
			but.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					final int chapter = ctr.d.maps.elementAt(index).chapter;
					//ctr.androidHelper.log("Step1. "+chapter);
					ctr.a.loadRSGame(chapter, false);
					getController().finishLoading();
					ctr.a.loadedGame();
					ctr.a.loadedMusic2();
					ctr.a.loadedRSGame(chapter, false);	
					Draw3Screen.INDEX_MAP = index;
					fadeOut(new TweenCallback() {
						@Override
						public void onEvent(int type, BaseTween<?> source) {
							ctr.a.bgSong.pause();
							ctr.a.clearDraw();
							clear();
							mController.setScreen(mController
									.getScreen("Draw3Screen"));											
						}
					});					
				}
			});		
			mapItem[index].setColor(1f, 1f, 1f, 0.0f);
			Tween.to(mapItem[index], WidgetAccessor.OPACITY, 1f).target(1f).ease(Quart.OUT).start(tweenManager);			
	}
	
	public void createIconScroll(){				
		if(iconTable == null){
			iconTable = ctr.stageHelper.createTableWithTransBg(ctr.g.mSkin, 0, 0, (140), (4*70));
			ImageButton noel = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);
			noel.addActor(ctr.stageHelper.createImage(ctr.d.getIconTemplate(7), 0, 0,70,70));			
			noel.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.maps.elementAt(currentItem-1).chapter == 7){
						return;
					}
					swipeAble = false;
					ctr.d.changeChapterMapDraw(currentItem-1, 7);
					xScrollBackup = (currentItem-1)*(520);
					count = 2;
					//System.out.println(xScrollBackup+"av");
					createMapScroll();
					
				}
			});		
			ImageButton candy = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);
			candy.addActor(ctr.stageHelper.createImage(ctr.d.getIconTemplate(0), 0, 0,70,70));			
			candy.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.maps.elementAt(currentItem-1).chapter == 0){
						return;
					}
					swipeAble = false;
					ctr.d.changeChapterMapDraw(currentItem-1, 0);
					xScrollBackup = (currentItem-1)*(520);
					count = 2;
					//System.out.println(xScrollBackup+"av");
					createMapScroll();
					
				}
			});				
			ImageButton school = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);			
			Image icon = ctr.stageHelper.createImage(ctr.d.getIconTemplate(1), 0, 0,70,70);
			if(ctr.d.chapters[1].isUnLock == false)
				icon.setColor(0.5f,0.5f,0.5f,1f);
			school.addActor(icon);			
			
			school.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.chapters[1].isUnLock == false){
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.chapterNotOpen, 1, null, null);
						return;
					}
					if(ctr.d.maps.elementAt(currentItem-1).chapter == 1){
						return;
					}
					ctr.d.changeChapterMapDraw(currentItem-1, 1);
					createMapScroll();
				}
			});		
			ImageButton vac = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);			
			Image ic2 = ctr.stageHelper.createImage(ctr.d.getIconTemplate(2), 0, 0,70,70);
			if(ctr.d.chapters[2].isUnLock == false)
				ic2.setColor(0.5f,0.5f,0.5f,1f);
			vac.addActor(ic2);			
			
			vac.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.chapters[2].isUnLock == false){
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.chapterNotOpen, 1, null, null);
						return;
					}
					if(ctr.d.maps.elementAt(currentItem-1).chapter == 2){
						return;
					}
					ctr.d.changeChapterMapDraw(currentItem-1, 2);
					createMapScroll();
				}
			});		
			ImageButton casi = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);			
			Image ic3 = ctr.stageHelper.createImage(ctr.d.getIconTemplate(3), 0, 0,70,70);
			if(ctr.d.chapters[3].isUnLock == false)
				ic3.setColor(0.5f,0.5f,0.5f,1f);
			casi.addActor(ic3);			
			
			casi.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.chapters[3].isUnLock == false){
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.chapterNotOpen, 1, null, null);
						return;
					}
					if(ctr.d.maps.elementAt(currentItem-1).chapter == 3){
						return;
					}
					ctr.d.changeChapterMapDraw(currentItem-1, 3);
					createMapScroll();
				}
			});				
			ImageButton bd = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);			
			Image ic4 = ctr.stageHelper.createImage(ctr.d.getIconTemplate(4), 0, 0,70,70);
			if(ctr.d.chapters[4].isUnLock == false)
				ic4.setColor(0.5f,0.5f,0.5f,1f);
			bd.addActor(ic4);			
			
			bd.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.chapters[4].isUnLock == false){
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.chapterNotOpen, 1, null, null);
						return;
					}
					if(ctr.d.maps.elementAt(currentItem-1).chapter == 4){
						return;
					}
					ctr.d.changeChapterMapDraw(currentItem-1, 4);
					createMapScroll();
				}
			});		
			ImageButton ot = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);			
			Image ic5 = ctr.stageHelper.createImage(ctr.d.getIconTemplate(5), 0, 0,70,70);
			if(ctr.d.chapters[5].isUnLock == false)
				ic5.setColor(0.5f,0.5f,0.5f,1f);
			ot.addActor(ic5);			
			
			ot.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.chapters[5].isUnLock == false){
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.chapterNotOpen, 1, null, null);
						return;
					}
					if(ctr.d.maps.elementAt(currentItem-1).chapter == 5){
						return;
					}
					ctr.d.changeChapterMapDraw(currentItem-1, 5);
					createMapScroll();
				}
			});		
			ImageButton love = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);			
			Image ic6 = ctr.stageHelper.createImage(ctr.d.getIconTemplate(6), 0, 0,70,70);
			if(ctr.d.chapters[6].isUnLock == false)
				ic6.setColor(0.5f,0.5f,0.5f,1f);
			love.addActor(ic6);			
			
			love.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.chapters[6].isUnLock == false){
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.chapterNotOpen, 1, null, null);
						return;
					}
					if(ctr.d.maps.elementAt(currentItem-1).chapter == 6){
						return;
					}
					ctr.d.changeChapterMapDraw(currentItem-1, 6);
					createMapScroll();
				}
			});
			iconTable.row();
			iconTable.add(noel);
			iconTable.add(candy);
			iconTable.row();
			iconTable.add(school);
			iconTable.add(vac);
			iconTable.row();
			iconTable.add(casi);
			iconTable.add(bd);
			iconTable.row();
			iconTable.add(ot);
			iconTable.add(love);
		}		
		if(iconScroll == null){
			iconScroll = new ScrollPane(iconTable);
			iconScroll.setBounds((0), (480-402+70-30-35-35+15), (140), (1.5f*70)+70);
			iconScroll.setScrollingDisabled(true, false);
		}		
	}

	boolean isShowTut = false;
	
	public void onManagedUpdate(float pDeltaTime)
	{		
		
		if(ctr.d.isHelpDraw1 && ctr.a.isHasDialog() == false){
			if(indexHelp == 0&&ctr.a.isHasDialog() == false){
				
			ctr.stageHelper.createHelpDraw11(stage, tweenManager, new IMAction() {				
				@Override
				public void action() {
					// TODO Auto-generated method stub					
					//indexHelp=1;
				}
			});			
			indexHelp = -10;
			}
//			else if(indexHelp == 1&&ctr.a.isHasDialog() == false){
//				ctr.stageHelper.createHelpDraw12(stage, tweenManager, new IMAction() {				
//					@Override
//					public void action() {
//						// TODO Auto-generated method stub					
//						indexHelp=2;
//					}
//				});			
//				indexHelp = -10;
//			}
//			else if(indexHelp == 2&&ctr.a.isHasDialog() == false){
//				ctr.stageHelper.createHelpDraw13(stage, tweenManager, new IMAction() {				
//					@Override
//					public void action() {
//						// TODO Auto-generated method stub					
//						indexHelp=3;
//					}
//				});			
//				indexHelp = -10;
//			}
//			else if(indexHelp == 3&&ctr.a.isHasDialog() == false){
//				ctr.stageHelper.createHelpDraw14(stage, tweenManager, new IMAction() {				
//					@Override
//					public void action() {
//						// TODO Auto-generated method stub					
//						indexHelp=4;
//					}
//				});			
//				indexHelp = -10;
//			}
//			else if(indexHelp == 4&&ctr.a.isHasDialog() == false){
//				ctr.stageHelper.createHelpDraw15(stage, tweenManager, new IMAction() {				
//					@Override
//					public void action() {
//						// TODO Auto-generated method stub					
//						indexHelp=5;
//					}
//				});			
//				indexHelp = -10;
//			}
//			else if(indexHelp == 5&&ctr.a.isHasDialog() == false){
//				ctr.stageHelper.createHelpDraw16(stage, tweenManager, new IMAction() {				
//					@Override
//					public void action() {
//						// TODO Auto-generated method stub					
//						indexHelp=6;
//					}
//				});			
//				indexHelp = -10;
//			}
//			else if(indexHelp == 6&&ctr.a.isHasDialog() == false){
//				ctr.stageHelper.createHelpDraw17(stage, tweenManager, new IMAction() {				
//					@Override
//					public void action() {
//						// TODO Auto-generated method stub					
//						ctr.d.isHelpDraw1 = false;
//						ctr.controller.getActivity().getPreference().saveValue("HelpDraw1", false);
//						indexHelp=-10;
//						
//					}
//				});			
//				indexHelp = -10;
//			}
		}
		
		update(pDeltaTime);
		
		if(currentItem+1>mapItem.length)
			next.setVisible(false);
		else{
			next.setVisible(true);
		}
		
		if(currentItem-1<1)
			prev.setVisible(false);
		else{
			prev.setVisible(true);
		}	
		
		if(swipeAble && mapScroll!=null)
		{			
			if(xScrollBackup >= 0 && count>0){
				mapScroll.setSmoothScrolling(false);
				count--;
				mapScroll.setScrollX(xScrollBackup);
				if(count == 0)
					xScrollBackup = -1;			
			}
			else{
				mapScroll.setSmoothScrolling(true);
			}
		if(
				mapScroll.isPanning()||
				mapScroll.isDragging()||
				mapScroll.isFlinging()
		  )
		{
			
		}		
		else
		{	
			scrollCurrentX = mapScroll.getScrollX();
			if(scrollCurrentX < (currentItem-1)*(520))
			{
				
				currentItem --;
				if(currentItem<1)
					currentItem = 1;
				mapScroll.setScrollX((currentItem-1)*(520));
			}
			else if(scrollCurrentX > (currentItem-1)*(520))
			{				
				currentItem ++;
				if(currentItem>mapItem.length)
					currentItem = mapItem.length;
				mapScroll.setScrollX((currentItem-1)*(520));
			}
		}
		}
		super.onManagedUpdate(pDeltaTime);
	}	
	
	public int[][] getMap(String s){
		int maxRow, maxCol;
		maxRow = 8;
		maxCol = 12;
		if(ctr.d.isLDPI){			
			maxCol = 10;
			maxRow = 6;
		}
		int[][] map = new int[maxRow][maxCol];
		int r,c;			
		r=0;
		c=0;	
			
		for(int i=0;i<s.length();i++){
			if(s.substring(i, i+1).equals("1")){
				map[r][c] = 1;
			}
			else{
				map[r][c] = -1;
			}
			c++;
			if(c==maxCol){
				c=0;
				r++;
			}
		}				
		return map;
	}
	public void clear(){
		super.clear();
		removeChild(paper);
		removeChild(bg);
		removeChild(painter);
		bg  = null;
		prev = null;
		paper = null;
		next = null;
		createMap = null;
		level = null;
		painter = null;
		mapTable = null;
		mapScroll = null;
		mapItem = null;	
		currentItem = 1;
		scrollCurrentX = 0;
				
		upgradeGome = null;
		cancel = null;
		iconTable = null;
		iconScroll = null;
		swipeAble = true;
		xScrollBackup = 0;	
	}
	
	public void backKeyProcess(){
		//postRunnable(new Runnable() {
			
		//	@Override
		//	public void run() {
				// TODO Auto-generated method stub
				fadeOut(new TweenCallback() {			
					@Override
					public void onEvent(int arg0, BaseTween<?> arg1) {
						// TODO Auto-generated method stub
						ctr.a.bgSong.pause();
						ctr.a.clearDraw();
						clear();
						ctr.a.loadedMenu();
						mController.setScreen(mController.getScreen("MenuScreen"));
					}
				});
		//	}
		//});		
	}
}
